package com.javajun.spider.bt7086.pipeline;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.javajun.spider.bt7086.entity.Video;
import com.javajun.spider.bt7086.repository.VideoRepository;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Component
public class VideoPipeline implements Pipeline{
	
	@Autowired
	private VideoRepository repository;
	
	private static VideoPipeline videoPipeline;
	
	
	@PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
	public void init() {
		videoPipeline = this;
		videoPipeline.repository = this.repository; 
	}
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		Video model = resultItems.get("model");
		if(model != null) {
			
			List<Video> videoList = videoPipeline.repository.findByUrl(model.getUrl());
			
			if(videoList.isEmpty()) {
				videoPipeline.repository.save(model);
			}
		}
	}

}
